﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PrzetwarzanieObrazow
{
    class FiltrowanieMaska
    {
        public UnsafeBitmap przetworz(UnsafeBitmap bmpin, int[,] maska, int maska_size)
        {
            UnsafeBitmap bmpinn = bmpin;
            int bmpinX = bmpin.Bitmap.Width;
            int bmpinY = bmpin.Bitmap.Height;
            int[,] bmptr = new int[bmpinX, bmpinY];
            int[,] bmptg = new int[bmpinX, bmpinY];
            int[,] bmptb = new int[bmpinX, bmpinY];
            int center = (maska_size + 1) / 2;
            int min=0, max=0;
            //sprawdzanie czy w ogóle opłaca się filtrować, jeżeli wielkość maski <= wielkości obrazka, to filtrujemy
            if (maska_size <= bmpinX && maska_size <= bmpinY)
            {

                //zacząć od x0 i y0 + center-1
                int xs = center - 1;
                int ys = center - 1;
                int xe = bmpinX - maska_size;
                int ye = bmpinY - maska_size;
                for (int i = xs;i<=xe;i++)
                    for(int j=ys;j<=ye;j++)
                    {
                        //przemnoz przez maske
                        int sumar=0;
                        int sumag=0;
                        int sumab=0;
                        int si = i - (center - 1);
                        int sj = j - (center - 1);
                        for(int ip = 0 ;ip < maska_size;ip++)
                        {
                            for( int jp =0;jp<maska_size;jp++)
                            {
                                sumar += bmpin.GetPixel(si, sj).red * maska[ip, jp];
                                sumag += bmpin.GetPixel(si, sj).green * maska[ip, jp];
                                sumab += bmpin.GetPixel(si, sj).blue * maska[ip, jp];
                                sj++;
                            }
                            sj = j - (center -1);
                            si++;
                        }
                        int colr = sumar/(maska_size*maska_size);
                        int colg = sumag/(maska_size*maska_size);
                        int colb = sumab/(maska_size*maska_size);
                        if (min > colr)
                            min = colr;
                        if (min > colg)
                            min = colg;
                        if (min > colb)
                            min = colb;

                        if (max < colr)
                            max = colr;
                        if (max < colg)
                            max = colg;
                        if (max < colb)
                            max = colb;
                        bmptr[i, j] = colr;
                        bmptg[i, j] = colg;
                        bmptb[i, j] = colb;
                        //bmpinn.SetPixel(i, j, Color.FromArgb(colr, colg, colb));
                    }
                int roznica = 0 - min;
                for (int i = 0; i < bmpinX; i++)
                {
                    for (int j = 0; j < bmpinY; j++)
                    {
                        bmptr[i, j] = bmptr[i, j] + roznica;
                        bmptg[i, j] = bmptg[i, j] + roznica;
                        bmptb[i, j] = bmptb[i, j] + roznica;
                    }
                }

                for (int i = 0; i < bmpinX; i++)
                {
                    for (int j = 0; j < bmpinY; j++)
                    {
                        double rd = bmptr[i, j];
                        double gd = bmptg[i, j];
                        double bd = bmptb[i, j];
                        bmptr[i, j] = (int)(rd / (max + roznica) * 255);
                        bmptg[i, j] = (int)(gd / (max + roznica) * 255);
                        bmptb[i, j] = (int)(bd / (max + roznica) * 255);
                    }
                }
                for (int i = 0; i < bmpinX; i++)
                {
                    for (int j = 0; j < bmpinY; j++)
                    {
                        PixelData px = new PixelData();
                        px.blue = (byte)bmptb[i, j];
                        px.green = (byte)bmptg[i, j];
                        px.red = (byte)bmptr[i, j];
                        bmpinn.SetPixel(i, j, px);
                    }
                        //res.SetPixel(i, j, Color.FromArgb(bmTemp[i, j].R, bmTemp[i, j].G, bmTemp[i, j].B));
                }

            }
            return bmpinn;
        }
    }
}
